package backend

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"miaoyuadmin/api/backend"
	"miaoyuadmin/api/backend/response"
	"miaoyuadmin/internal/enums/roleenum"
	"miaoyuadmin/internal/model"
	"miaoyuadmin/internal/service"
	"strings"
)

type roleCtl struct {
}

var Role = roleCtl{}

func (c *roleCtl) Index(ctx context.Context, req *backend.RoleIndexReq) (res *backend.RoleIndexRes, err error) {
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl": "role/index.html",
		"footTpl": "role/index.js.html",
		"title":   "职级管理",
	})

	return
}
func (c *roleCtl) Add(ctx context.Context, req *backend.RoleAddReq) (res *backend.RoleAddRes, err error) {
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl":    "role/add.html",
		"footTpl":    "role/add.js.html",
		"siteName":   "控制台",
		"statusList": roleenum.ListStatusMap,
	})

	return
}
func (c *roleCtl) Grant(ctx context.Context, req *backend.RoleGrantReq) (res *backend.RoleGrantRes, err error) {
	//获取所有菜单以及该角色的菜单
	roleId := req.ResId
	menuList, resErr := service.Menu().GetMenuListJoinRole(ctx, roleId)
	if resErr != nil {
		err = resErr
		return
	}

	jsonStr, _ := gjson.EncodeString(menuList)

	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl":      "role/grant.html",
		"footTpl":      "role/grant.js.html",
		"siteName":     "控制台",
		"jsonListData": jsonStr,
		"resId":        roleId,
	})

	return
}
func (c *roleCtl) GrantSubmitDo(ctx context.Context, req *backend.RoleGrantSubmitDoReq) (res *backend.RoleGrantSubmitDoRes, err error) {
	resId := req.ResId
	menuList := req.MenuList
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	_, resErr := service.Role().GetItemById(ctx, resId)

	if resErr != nil {
		commonJson.SetErrorJsonExit("请求数据不存在，请确认后提交！")
	}
	fmt.Println("提交菜单列表", menuList)
	_, sqlErr := service.Role().Grant(ctx, resId, menuList)
	if sqlErr != nil {
		commonJson.SetErrorJsonExit(sqlErr.Error())
	}
	commonJson.SetSuccessJsonExit("提交成功", nil)

	return
}
func (c *roleCtl) Edit(ctx context.Context, req *backend.RoleEditReq) (res *backend.RoleEditRes, err error) {
	resId := req.ResId

	itemRes, resErr := service.Role().GetItemById(ctx, resId)
	if resErr != nil {
		err = resErr
		response.UniResp(g.RequestFromCtx(ctx), err.Error(), "")
		return
	}
	response.BuildTpl(g.RequestFromCtx(ctx), "public/layout.html").WriteTpl(g.MapStrAny{
		"mainTpl":    "role/add.html",
		"footTpl":    "role/edit.js.html",
		"siteName":   "控制台",
		"data":       itemRes,
		"statusList": roleenum.ListStatusMap,
	})
	return
}

func (c *roleCtl) DelDo(ctx context.Context, req *backend.RoleDelDoReq) (res *backend.RoleDelDoRes, err error) {
	resId := req.ResId
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	_, resErr := service.Role().Del(ctx, resId)
	if resErr != nil {
		err = resErr
		commonJson.SetErrorJsonExit(err.Error())
		return
	}
	commonJson.SetSuccessJsonExit("操作成功", nil)
	return

}

func (c *roleCtl) ListDo(ctx context.Context, req *backend.RoleListDoReq) (res *backend.RoleListDoRes, err error) {

	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	listOptions := model.RoleListInput{Page: 1, Limit: 10}
	if req.Page <= 0 {
		listOptions.Page = 1
	} else {
		listOptions.Page = req.Page
	}
	if req.Limit <= 0 {
		listOptions.Limit = 10
	} else {
		listOptions.Limit = req.Limit
	}
	if req.Keywords = strings.Trim(req.Keywords, " "); req.Keywords != "" {
		listOptions.Keywords = req.Keywords
	}
	listRes, count, resErr := service.Role().GetPageList(ctx, listOptions)
	if resErr != nil {
		err = resErr
		return
	}
	resData := response.JsonResult{Data: listRes, Count: count, Msg: "ok"}
	commonJson.SetSuccessMapExit(resData)

	return
}
func (c *roleCtl) SubmitDo(ctx context.Context, req *backend.RoleSubmitDoReq) (res *backend.RoleSubmitDoRes, err error) {
	var submitInput model.RoleSubmitInput
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	err = gconv.Scan(req, &submitInput)
	if err != nil {
		return
	}
	resId := req.ResId
	if resId != 0 {
		_, err = service.Role().Edit(ctx, submitInput, resId)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		} else {
			commonJson.SetSuccessJsonExit("修改成功", nil)
		}
		return
	} else {
		_, err = service.Role().Add(ctx, submitInput)
		if err != nil {
			commonJson.SetErrorJsonExit(err.Error())
		}

		commonJson.SetSuccessJsonExit("添加成功", nil)
	}

	return
}
